package code.easylevel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Ashish Sonawane
 * Your program should accept a path to a file as its first argument.
 * The file contains multiple lines. The first line indicates the number of lines you should output,
 * the other lines are of different length and are presented randomly.
 * You may assume that the input file is formatted correctly and the number in the first line is a valid positive integer.
 *
 */
public class LongestLines {

	public static void main(String[] args) throws IOException {
		BufferedReader bufferedReader;
		String line;
		File file = new File(args[0]);
		bufferedReader = new BufferedReader(new FileReader(file));
		Integer lineNo = new Integer(1);
		int numberOfRecordsShow = 0;
		List<String> lineList = new ArrayList<>();
		while((line=bufferedReader.readLine()) != null){
			if(lineNo>1){
				lineList.add(line);
			}else{
				numberOfRecordsShow=Integer.parseInt(line);
			}
			lineNo++;
		}	
		lineList = findLongestLine(lineList);
		Collections.reverse(lineList);
		for(int i = 0; i<=numberOfRecordsShow-1;i++){
			System.out.println(lineList.get(i));
		}
		bufferedReader.close();
	}
	
	static List<String> findLongestLine(List<String> unsortedList){
		String temp;
		for (int i = 0; i < unsortedList.size(); i++) {
            for(int j = i ; j > 0 ; j--){
                if(unsortedList.get(j).toString().length() < unsortedList.get(j-1).toString().length()){
                    temp = unsortedList.get(j).toString();
                    unsortedList.set(j,unsortedList.get(j-1));
                    unsortedList.set(j-1,temp );
                }
            }
        }
		return unsortedList;

	}

}
